package org.example.domain.query;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "主页数据查询条件")
public class HomeQuery{
    
    @ApiModelProperty("今天开始时间")
    private DateTime beginDate;
    
    @ApiModelProperty("今天结束时间")
    private DateTime endDate;
    
    @ApiModelProperty("昨天开始时间")
    private DateTime yestBeginDate;
    
    @ApiModelProperty("昨天结束时间")
    private DateTime yestEndDate;
    
    @ApiModelProperty("这个月开始时间")
    private DateTime firstMonthDate;
    
    @ApiModelProperty("这个月结束时间")
    private DateTime lastMonthDay;
    
    @ApiModelProperty("今年时间")
    private int year;
    
    @ApiModelProperty("租户id")
    private Long tenantId;
    
    
    public static HomeQuery of(Long tenantId){
        // 今天时间开始
        DateTime beginDate = DateUtil.beginOfDay(new Date());
        // 今天时间结束
        DateTime endDate = DateUtil.endOfDay(beginDate);
        // 昨天时间开始
        DateTime yestBeginDate = DateUtil.offsetDay(beginDate, -1);
        // 昨天时间结束
        DateTime yestEndDate = DateUtil.offsetDay(endDate, -1);
        // 这个月开始时间
        LocalDate date = LocalDate.now();
        LocalDate firstMonthDay = date.with(TemporalAdjusters.firstDayOfMonth()); // 获取第一天
        DateTime firstMonthDate = DateUtil.beginOfDay(DateUtil.parse(firstMonthDay.toString()));
        // 这个月结束时间
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());// 获取最后一天
        DateTime lastMonthDay = DateUtil.endOfDay(DateUtil.parse(lastDay.toString()));
        // 今年时间
        int year = DateUtil.year(beginDate);
        
        return new HomeQuery(beginDate, endDate, yestBeginDate, yestEndDate, firstMonthDate, lastMonthDay, year, tenantId);
    }
}
